//用户 个人信息（需重新设计）

interface IUserViewModel extends ng.IScope {
    /**
     * 保存用户信息
     */
    save: () => void;
    /**
     * 用户名
     */
    userName: string;
    /**
     * 真实姓名
     */
    realName: string;
    /**
     * 所属部门
     */
    department: string;
    /**
     * 原密码
     */
    userPassword: string;
    /**
     * 重置密码
     */
    RuserPassword: string;
    /**
     * 重复重置密码
     */
    reUserPassword: string;

    /**申领
     * 原密码 */
    oldPassword:string;

    /**重置密码 */
    password:string;

    /**重复重置密码 */
    rePassword:string;

     /**
     * 保存申领密码
     */
    passwordSave: () => void;

    headImgUploader:any;

    saveHead:()=>void;
    /**
     * 头像的base64
     */
    base64:string;
    
}

class UserController {
    id: string;
    uploaderList: AddUserInfoAttachmentDetail[] = [];
    // uploaderList=[];
    constructor(private $scope: IUserViewModel, private userService: IUserService, private $ngBootbox: BootboxService,private routeService:routeService,
        private commonService:commonService ,private $rootScope:ImainRootScope,private ossUploaderService:ossUploaderService) {
        this.init();
    }

    private init(): void {

        this.id = JSON.parse(window.localStorage.getItem("loginData").toString()).id;
        this.$scope.realName = JSON.parse(window.localStorage.getItem("loginData").toString()).realName;
        this.$scope.department = JSON.parse(window.localStorage.getItem("loginData").toString()).userGroup;
        this.userService.getDetail(this.id).then(result => {
            this.$scope.userName = result.data.userName;

            //头像（登录时获取详情头像）
        });

        this.$scope.save = this.saveUser;

        const OssFile = this.ossUploaderService.getOssFile();

        this.$scope.headImgUploader =new OssFile(this.$scope,this.$scope.headImgUploader,{uploaderList:this.uploaderList,tag:1});



      
        this.$scope.headImgUploader.onSuccess = (fileItem: any) => { this.onSuccessItem(fileItem.fileName, fileItem.imgSrc, 1, fileItem._file); }
       
        this.$scope.headImgUploader.onAfterAddingFile = this.handleAddingFile;

        this.$scope.saveHead=this.saveHead;

    }



    private saveUser = () => {
        var userPassword = this.$scope.userPassword;
        var RuserPassword = this.$scope.RuserPassword;
        if (userPassword == RuserPassword) {
            this.$ngBootbox.alert("新旧密码一样，请重新输入新密码");
            this.$scope.RuserPassword = "";
            this.$scope.reUserPassword = "";
            return;
        }

        this.userService.updatePassword(this.id, this.$scope.userPassword, this.$scope.RuserPassword, this.$scope.reUserPassword,[]).then((res) => {
            this.$ngBootbox.alert("修改成功！");
            this.$scope.userPassword = "";
            this.$scope.RuserPassword = "";
            this.$scope.reUserPassword = "";
            //存储下修改后的头像
        }, (rel) => {
            this.$scope.userPassword = "";
            this.$scope.RuserPassword = "";
            this.$scope.reUserPassword = "";
        })

    }
    handleAddingFile = (item) => {
        
        
        var file=new FileReader();
     
        


        const ALLOWFILETYPE=['jpg','jpeg','png','gif','tiff']
        if(item._file.size > 5*1024*1024){
            this.$ngBootbox.confirm('文件大小不能超过5M').then(result => {
                this.onDelete(item);
                return;
            },reject => {
                this.onDelete(item);;
            });
            
        }else {
            var fileType = item._file.name.split('.').pop();
            if(ALLOWFILETYPE.indexOf(fileType.toLowerCase()) === -1){
                this.$ngBootbox.confirm('不支持该文件类型').then(result => {
                    this.onDelete(item);;
                },reject=>{
                    this.onDelete(item);;
                })
            }else{
                file.readAsDataURL(item._file);
                file.onload=()=>{
                    
                    this.$scope.base64=file.result;
                    if (!this.$scope.$$phase) {
                        this.$scope.$apply(() => { this.$scope.base64 });
                    }
                }
            }
        }

        
    }
    /**
     * 选择上传文件后
     */
    onSuccessItem = (fileName: string, filePath: string, tag: number, file?: any) => {
        file.fileId = filePath;
        let attachment = {
            attachmentId: "",
            attachmentName: fileName,
            attachmentPath: filePath,
            attachmentTag: tag.toString()
        };    
        
        this.uploaderList.push(attachment);
        this.saveHead();
    }
    /**
     * 删除附件
     */
    onDelete = (item: any) => {
       
        // this.commonService.onDelete(item, this.uploaderList);
        this.$scope.headImgUploader.onDelete(item)
        this.$scope.base64="";
        this.uploaderList=[];
        this.$scope.headImgUploader.clearQueue();
        if (!this.$scope.$$phase) {
            this.$scope.$apply(() => { this.$scope.base64 });
        }
    }
    saveHead = ()=>{
        this.userService.updatePassword(this.id, null, null, null,this.uploaderList).then((res) => {
            this.$ngBootbox.alert("修改头像成功！");
            
            //存储下修改后的头像
            this.$rootScope.headPath=res.data.extData;
            window.localStorage.setItem('headPath',this.$rootScope.headPath);
            this.uploaderList=[];
            this.$scope.headImgUploader.clearQueue();
            this.$scope.base64=""
        }, (rel) => {
            this.uploaderList=[];
            this.$scope.headImgUploader.clearQueue();
            this.$scope.base64=""
        })
    }
}

angular.module("tms").controller("userController", ["$scope","userService", "$ngBootbox","routeService","commonService","$rootScope","ossUploaderService", UserController]);


/**申领密码 */
class ApplyPassWord{
    id:string;
  

    constructor(private $scope: IUserViewModel, private userService: IUserService, private $ngBootbox: BootboxService) {
        this.init();
    }

     private init(): void {
        this.id = JSON.parse(window.localStorage.getItem("loginData").toString()).id;
        this.$scope.passwordSave = this.saveUser;

        


    }

     private saveUser = () => {
        this.userService.updateApplyPassword(this.id,this.$scope.oldPassword,this.$scope.password,this.$scope.rePassword).then((res) => {
            //console.log(res);
            this.$ngBootbox.alert("修改成功！");
          
        })
     }

}
angular.module("tms").controller("applyPassWord", ["$scope","userService", "$ngBootbox", ApplyPassWord]);
